/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import { expect } from '@jest/globals';
import { iconv } from '@ohos/hvigor';
import fse from 'fs-extra';
import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { resourcesPath } from '../../plugins/common-fun';

import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  for (let i = 1; i <= 20; i++) {
    const libraryPath = path.resolve(projectRootPath, `har${i}`);
    await fse.copy(path.resolve(resourcesPath, 'extra/staticlibrary'), libraryPath);
    const libraryMain = path.resolve(libraryPath, 'src/main');
    await fse.ensureFile(`${libraryMain}/module.json5`);
    await fse.writeFile(`${libraryMain}/module.json5`,
     JSON.stringify({ module: { name: `har${i}`, type: 'har', deviceTypes: ['phone', 'tablet'] } }, null, 2), 'utf-8');
  }
  const moduleFile = await fse.readFile(`${projectRootPath}/build-profile.json5`);
  // 去掉文件中多余的 ,
  const str = iconv.decode(moduleFile, 'utf-8').replace(/(,[\r\n]\s+[}|\]])/g, ($1) => $1.slice(1));
  const moduleObj = JSON.parse(str);
  for (let i = 1; i <= 20; i++) {
    moduleObj.modules.push({
      name: `har${i}`,
      srcPath: `./har${i}`,
    });
  }

  await fse.writeFile(`${projectRootPath}/build-profile.json5`, JSON.stringify(moduleObj, null, 2), 'utf-8');

  // 执行hap构建
  const commonCommand = '--mode module -p product=default -p module=entry@default,har1@default,har2@default,har3@default,har4@default,har5@default,har6@default,har7@default,har8@default,har9@default,har10@default,har11@default,har12@default,har13@default,har14@default,har15@default,har16@default,har17@default,har18@default,har19@default,har20@default assembleHap assembleHar --analyze=normal --parallel --incremental --no-daemon'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commonCommand);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('DTS2024071728697')
  .description('多模块har工程构建测试用例')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();
